    //
//  SecondVC.m
//  DogLocate
//
//  Created by mac on 11-5-23.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SecondVC.h"
#import "Coordinate.h"


@implementation SecondVC

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/
-(id)initWithLocationArray :(NSArray *)locations {

	if (self = [super init]) {
		
		map = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
		map.delegate = self;
		map.mapType = MKMapTypeStandard;
		map.zoomEnabled = YES;
		map.scrollEnabled = YES;
		[self.view addSubview:map];
		
		for (Coordinate *coor in locations) {
		
			CLLocationCoordinate2D locate;
			locate.latitude = [coor.latitude doubleValue];
			locate.longitude = [coor.longitude doubleValue];
			
			[self updateMapWithCoordinate:locate];
		}
		
		
	}
	
	return self;
}

-(void)updateMapWithCoordinate:(CLLocationCoordinate2D)location {

	if (!crumbs)
	{
		// This is the first time we're getting a location update, so create
		// the CrumbPath and add it to the map.
		//
		crumbs = [[CrumbPath alloc] initWithCenterCoordinate:location];
		[map addOverlay:crumbs];
		
		// On the first location update only, zoom map to user location
		MKCoordinateRegion region = 
		MKCoordinateRegionMakeWithDistance(location, 2000, 2000);
		[map setRegion:region animated:YES];
	}
	else
	{
		// This is a subsequent location update.
		// If the crumbs MKOverlay model object determines that the current location has moved
		// far enough from the previous location, use the returned updateRect to redraw just
		// the changed area.
		//
		// note: iPhone 3G will locate you using the triangulation of the cell towers.
		// so you may experience spikes in location data (in small time intervals)
		// due to 3G tower triangulation.
		// 
		MKMapRect updateRect = [crumbs addCoordinate:location];
		
		if (!MKMapRectIsNull(updateRect))
		{
			// There is a non null update rect.
			// Compute the currently visible map zoom scale
			MKZoomScale currentZoomScale = (CGFloat)(map.bounds.size.width / map.visibleMapRect.size.width);
			// Find out the line width at this zoom scale and outset the updateRect by that amount
			CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
			updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);
			// Ask the overlay view to update just the changed area.
			[crumbView setNeedsDisplayInMapRect:updateRect];
		}
	}
		
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    if (!crumbView)
    {
        crumbView = [[CrumbPathView alloc] initWithOverlay:overlay];
    }
    return crumbView;
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
